<template>
  <div class="cont">
    <ul class="nav">
      <!-- 侧边栏的渲染 -->
      <div class="zhanwei"></div>
      <!-- 为v-for列表添加渲染动画 -->
      <TransitionGroup name="list">
        <li v-for="item in homelist"
          :key="item.index"
          @click="checkWord(item.index)"
          :class="{active:activeNum===item.index}">
          <i class="iconfont" :class="item.icon"></i>
          {{item.name}}
          <div class="triangle"></div>
        </li>
      </TransitionGroup>
    </ul>

    <div>
      <!-- 轮播图 -->
      <carousel></carousel>
      <!-- 轮播图 -->
      <div v-for="item in homelist" :key="item.index" v-show="activeNum===item.index">
        <div class="viewheader">{{item.name}}</div>
        <div class="viewbody">
          <TransitionGroup name="item">
            <router-link class="viewitem"
            v-for="son in childrenList" :to="{path: 'word', query: {name: son.params }}" :key="son.id">
            <div class="title">
              <span>{{son.name}}</span>
            </div>
            <div class="content">
              <div>{{son.wordnum}}词</div>
              <div>{{son.coursnum}}课</div>
            </div>
          </router-link>
          </TransitionGroup>
        </div>
      </div>
    </div>

    <div class="right">
      <newMessage></newMessage>
      <hotWordlist></hotWordlist>
    </div>
  </div>
</template>
<script>
import carousel from '@/components/Carousel.vue'
import newMessage from './newMessage.vue'
// 热门词表
import hotWordlist from './hotWordlist.vue'
import { getHomeList } from '@/api/home'
export default {
  name: 'homenav-vue',
  components: {
    carousel,
    newMessage,
    hotWordlist
  },
  data () {
    return {
      activeNum: 1,
      homelist: [],
      childrenList: []
    }
  },
  methods: {
    checkWord (id) {
      this.activeNum = id
      const num = id - 1
      let child = this.homelist[num].children
      child = JSON.parse(child)
      this.childrenList = child
      // console.log(this.childrenList)
    }
  },
  setup () {

  },
  created () {
    getHomeList().then(res => {
      this.homelist = res.data
      let child = res.data[0].children
      child = JSON.parse(child)
      this.childrenList = child
    })
  }
}
</script>
<style scoped lang="less">
.cont{
  border-left: 1px solid rgb(232, 229, 229);
  border-right: 1px solid rgb(232, 229, 229);
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: row;
  .right{
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 358px;
    padding-left: 20px;
    padding-top: 20px;
  }
}
.nav{
  background-color: rgb(232, 229, 229);
  width: 200px;
  height: 100%;
  .zhanwei{
    height: 150px;
  }
  li{
    width: 200px;
    margin-bottom: 10px;
    line-height: 40px;
    list-style: none;
    height: 40px;
    text-indent: 20px;
    cursor: pointer;
  }
  .active{
    background-color: #3e87e5;
    color: white;
    border-radius: 0, 0, 10%,10%;
    .triangle {
      position: relative;
      left:100%;
      bottom: 40px;
      width: 0;
      height: 0;
      border-left: 20px solid #3e87e5;
      border-top: 20px solid transparent;
      border-bottom: 20px solid transparent;
  }
  }
}
.viewheader{
  height: 40px;
  line-height: 40px;
  padding-left: 20px;
  font-size: 16px;
  color: #3e87e5;
}

.viewbody{
  background-color: #fff;
  height: 100%;
  width: 640px;
  display: flex;
  flex-wrap: wrap;
  .viewitem{
    margin-top: 20px;
    height: 80px;
    width: 23%;
    background-color: rgb(232, 229, 229);
    &:hover{
      background-color: white;
      border: 1px solid rgb(232, 229, 229);
      box-shadow: 10px 10px 5px rgba(91, 91, 91, 0.7);
      cursor: pointer;
    }
    margin-left: 10%;
    .title{
      font-weight: 600;
      margin-top: 15px;
      width: 100%;
      text-align: center;
      //文本溢出显示省略号
      overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    }
    .content{
      margin-top: 10px;
      display: flex;
      div{
        color: rgb(109, 106, 106);
        width: 50%;
        font-size:12px;
        text-align: center;
      }
    }
  }
}

//为v-for列表添加渲染动画
.list-move, /* 对移动中的元素应用的过渡 */
.list-enter-active,
.list-leave-active {
  transition: all 0.5s ease;
}
.list-enter-from,
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
}
/* 确保将离开的元素从布局流中删除
  以便能够正确地计算移动的动画。 */
.list-leave-active {
  position: absolute;
}

.item-enter-active,
.item-leave-active {
  transition: all 0.5s ease;
}
.item-enter-from,
.item-leave-to {
  opacity: 0;
}
</style>
